<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/13
 * Time: 14:39
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Dyn extends Base
{
    /**
     * 首页
     */
    public function index(){
        if(request()->isAjax()){
            $user_name = trim(input('user_name'), ' ');
            $ct_stats = input('ct_stats');
            $where = [];
            if(isset($user_name) && $user_name != ''){
                $where['user_username|h_title'] = ['like', '%' . $user_name . '%'];
            }
            if(isset($ct_stats) && $ct_stats != ''){
                $where['h_status'] = $ct_stats;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_hot h')
                ->join('up_u_user u', 'h.h_user_id = u.user_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_hot h')
                ->join('up_u_user u', 'h.h_user_id = u.user_id', 'left')
                ->field('h.*, user_username, user_phone, user_piano')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('h_status asc,h_time desc')
                ->select();
            foreach ($lists as $key => $vo){
                $lists[$key]['panios'] = '';
                $panios = Db::table('up_piano')->where('pa_id','in',trim($vo['user_piano'],','))->column('pa_name');
                if($panios){
                    $lists[$key]['panios'] = implode($panios,',');
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 帖子详情
     * @return mixed
     */
    public function dynDetail(){
        $h_id = input('h_id');
        $detail = Db::table('up_hot h')
            ->join('up_u_user u', 'h.h_user_id = u.user_id', 'left')
            ->field('h.*, user_username, user_phone, user_piano')
            ->where('h_id', $h_id)
            ->find();
        if($detail['h_url'] && $detail['h_type'] == 1){
            $img = trim($detail['h_url'], ',');
            $detail['h_url'] = explode(',', $img);
        }
        $detail['panios'] = '';
        $panios = Db::table('up_piano')->where('pa_id','in',trim($detail['user_piano'],','))->column('pa_name');
        if($panios){
            $detail['panios'] = implode($panios,',');
        }
        $this->assign('detail', $detail);
        return $this->fetch('dyn/dyndetail');
    }
    
    /**
     * 删除帖子
     * @return \think\response\Json
     */
    public function dynDel(){
        $h_id = input('id');
        $res = Db::table('up_hot')->delete($h_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 同意
     * @return \think\response\Json
     */
    public function agree(){
        $h_id = input('id');
        $res = Db::table('up_hot')->where('h_id',$h_id)->setField('h_status',2);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }
    /**
     * 拒绝
     * @return \think\response\Json
     */
    public function refuse(){
        $h_id = input('id');
        $res = Db::table('up_hot')->where('h_id',$h_id)->setField('h_status',3);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }
    

}